<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="角色码值" prop="role">
      <el-input v-model="dataForm.role" placeholder="角色码值"></el-input>
    </el-form-item>
    <el-form-item label="角色描述" prop="roleDes">
      <el-input v-model="dataForm.roleDes" placeholder="角色描述"></el-input>
    </el-form-item>
    <el-form-item label="角色权限" prop="roleRight">
      <el-input v-model="dataForm.roleRight" placeholder="角色权限"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="gmtCreate">
      <el-input v-model="dataForm.gmtCreate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="gmtModified">
      <el-input v-model="dataForm.gmtModified" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="0代表不展示，1代表展示" prop="ifShow">
      <el-input v-model="dataForm.ifShow" placeholder="0代表不展示，1代表展示"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除（0代表未删除，1代表删除）" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="逻辑删除（0代表未删除，1代表删除）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          role: '',
          roleDes: '',
          roleRight: '',
          gmtCreate: '',
          gmtModified: '',
          ifShow: '',
          isDeleted: ''
        },
        dataRule: {
          role: [
            { required: true, message: '角色码值不能为空', trigger: 'blur' }
          ],
          roleDes: [
            { required: true, message: '角色描述不能为空', trigger: 'blur' }
          ],
          roleRight: [
            { required: true, message: '角色权限不能为空', trigger: 'blur' }
          ],
          gmtCreate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          ifShow: [
            { required: true, message: '0代表不展示，1代表展示不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '逻辑删除（0代表未删除，1代表删除）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/security/bloodrole/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.role = data.bloodRole.role
                this.dataForm.roleDes = data.bloodRole.roleDes
                this.dataForm.roleRight = data.bloodRole.roleRight
                this.dataForm.gmtCreate = data.bloodRole.gmtCreate
                this.dataForm.gmtModified = data.bloodRole.gmtModified
                this.dataForm.ifShow = data.bloodRole.ifShow
                this.dataForm.isDeleted = data.bloodRole.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/security/bloodrole/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'role': this.dataForm.role,
                'roleDes': this.dataForm.roleDes,
                'roleRight': this.dataForm.roleRight,
                'gmtCreate': this.dataForm.gmtCreate,
                'gmtModified': this.dataForm.gmtModified,
                'ifShow': this.dataForm.ifShow,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
